﻿package com.pandemicode.framework.localization {
	import flash.events.Event;
	import com.pandemicode.framework.interfaces.IDisposeable;
	import flash.text.TextField;
	import flash.text.TextFormat;
	
	/**
	* @author Michael Lawrence
	* @version 1.0.0
	*/
	public class LocalizedTextFieldMap implements IDisposeable {
		private var _textField:TextField;
		private var _stringID:String;
		
		/**
		 * Maps a TextField to a string ID in the LocaleManager.
		 * @param	textField	The TextField to map.
		 * @param	stringID	The ID of the string in the LocaleManager to use.
		 */
		public function LocalizedTextFieldMap(textField:TextField, stringID:String) {
			this._textField = textField;
			this._stringID = stringID;
			
			LocaleManager.instance.changed.add(this._onLocaleChanged);
		}
		
		/**
		 * Disposes of the class.
		 */
		public function dispose():void {
			this._textField = null;
			this._stringID = null;
		}
		
		/**
		 * Updates the TextField's font and text.
		 */
		private function _update():void {
			if(LocaleManager.instance.currentLocale != null) {
				var tf:TextFormat = this._textField.getTextFormat();
				if (tf == null)
					tf = new TextFormat();
				tf.font = LocaleManager.instance.currentLocale.font.fontName;
				
				this._textField.defaultTextFormat = tf;
				this._textField.setTextFormat(tf);
				
				this._textField.text = LocaleManager.instance.getString(this._stringID);
			}
		}
		
		/**
		 * Occurs when the locale changes.
		 * @param	e	The dispatched event.
		 */
		private function _onLocaleChanged(locale:Locale):void {
			this._update();
		}
	}
}
